import { isNumber, intervalTime, notChanges, unique, send } from '@/utils/websocket'
// import { get } from 'core-js/fn/dict'
// import enumData from '@/utils/enum.json' // 枚举
// import statusPage from '@/components/mainboard/listStatusTip'

// 10进制转为16进制
export function to16(num1) {
  const num = num1
  return num.toString(16)
}

// 提交成功-根据id重新获取值
export function againData(_this, target, currentIndex) {
  const list = []
  if (currentIndex !== null) {
    for (const k in target) {
      list.push({
        command_id: k.replace('R_', ''),
        value: ''
      })
    }
  }
  if (list.length > 0) {
    // 发送数据
    send(_this, '01', list)
  }
}

// 控制面板切换和信息面板展示是否符合要求 符合继续
export function isRequire(_this) {
  // 判断交流和直流控制面板的选中项是否为空
  if (_this.currentDcIndex !== '') {
    // 1. 判断信息面板是否在加载中
    if (_this.dcContentLoad) {
      return true
    }
    // 2. 判断直流通道按钮是否有加载样式
    for (let i = 0; i < _this.DcIcon.length; i++) {
      if (_this.DcIcon[i][`icon${_this.currentDcIndex}`] !== '') {
        return true
      }
    }
  }
  if (_this.currentAcIndex !== '') {
    // 1. 判断信息面板是否在加载中
    if (_this.acContentLoad) {
      return true
    }
    // 2. 判断交流通道按钮是否有加载样式
    for (let i = 0; i < _this.AcIcon.length; i++) {
      if (_this.AcIcon[i][`icon${_this.currentAcIndex}`] !== '') {
        return true
      }
    }
  }
}

// 写入交流/直流控制面板数据
export function getKZMB(_this, list, type) {
  // 获取控制面板方法
  list.forEach((item, index) => {
    for (let j = 0; j < _this[type].length; j++) {
      for (const k in _this[type][j]) {
        if (k === `R_${item.command_id.toLowerCase()}`) {
          if (isNumber(item.value)) {
            // console.log(_this[type][j][k], Number(item.value))
            _this[type][j][k] = Number(item.value)
          } else {
            _this[type][j][k] = item.value
          }
          if (type === 'Dc') {
            _this.till_1 = false
          } else {
            _this.till_2 = false
          }
        }
      }
    }
  })
  // 加载时设置默认选中项
  if (_this.currentAcIndex === 0) {
    initAcSwitch(_this, _this.currentAcIndex)
  }
}

// 填充信息面板数据
export function getXXMB(_this, id, content, type) {
  const Index = _this[`current${type}Index`]
  const list = _this[`${type}Content`][`${Index}`]
  const typeCase = type.toLowerCase()
  list.forEach((item, index) => {
    for (const k in item) {
      if (k === `R_${id}`) {
        item[k] = content
      }
      if (type === 'Dc') {
        if (k === `R_000${_this.DcArr[_this.currentDcIndex]}000b`) {
          // 连续时间
          if (content === '') {
            return
          }
          item[k] = content.replace('S', '')
        }
        if (k === `R_000${_this.DcArr[_this.currentDcIndex]}0001`) {
          // 设备保护最小电流
          if (content === '') {
            return
          }
          item[k] = content.replace('mA', '')
        }
        if (k === `R_000${_this.DcArr[_this.currentDcIndex]}0002`) {
          // 设备保护最大电流
          if (content === '') {
            return
          }
          item[k] = content.replace('mA', '')
        }
      }
    }
  })
  setTimeout(() => {
    _this[`${typeCase}ContentLoad`] = false
  }, 200)
  // 设置按钮加载状态和disable
  // 如果没有显示信息面板 return
  if (!_this[`is${type}Infor`]) {
    return
  }
  switch (type) {
    case 'Ac':
      var acIndex = _this.currentAcIndex
      _this.AcDisable[0][`d${acIndex}`] = false
      // 初始开关disable状态
      if (acIndex !== '') {
        initAcSwitch(_this, acIndex)
      }
      break
    case 'Dc':
      var dcIndex = _this.currentDcIndex
      _this.DcDisable[0][`d${dcIndex}`] = false
      // 初始开关disable状态
      if (dcIndex !== '') {
        initDcSwitch(_this, dcIndex)
      }
      break
    default:
  }
}

// 提交按钮函数
export function submitBtn(_this, index, count, type) {
  if (count === 1) {
    _this.submitBtn = true
  }
  _this.bindFlag = true
  const typeCase = type.toLowerCase() // type转小写
  // 1.先清除上一次定时器
  clearTimeout(_this.timeHandle)
  // 2. 获取更改项id并去重
  const arr = unique(_this[`selected${count + 1}`])
  // 3.判断数组长度 小于1 没有更改项
  if (arr.length < 1) {
    _this.$message.warning(notChanges)
    return
  }
  _this.clickType = 1 // 判断是提交按钮还是开关
  _this.count = count
  // 信息面板加载中样式
  _this[`${typeCase}ContentLoad`] = true
  _this.AcDisable.forEach(item => {
    item[`d${index}`] = true
  })
  const list = []
  for (let i = 0; i < arr.length; i++) {
    list.push({
      command_id: arr[i].replace('R_', ''),
      value: _this[`${type}Content`][index][count][arr[i]]
    })
  }
  // 发送数据
  send(_this, '02', list)
  // 提交按钮
  _this.timeHandle = setTimeout(() => {
    if (_this[`${typeCase}ContentLoad`]) {
      _this[`${typeCase}ContentLoad`] = false
      _this.AcDisable.forEach(item => {
        item[`d${index}`] = false
      })
      if (count === 1) {
        _this.submitBtn = false
      }
      _this.bindFlag = false
      _this.$message.error('修改失败 稍后重试')
    }
  }, intervalTime)
}

// 监听交流/直流选中项按钮的样式
export function watchCurrentIndex(_this, newVal, type, type2) {
  if (_this.type === type) {
    for (let i = 0; i < _this[`${type}Disable`].length; i++) {
      for (const k in _this[`${type}Disable`][i]) {
        if (k === 'd' + newVal) {
          _this[`${type}Disable`][i][k] = false
        } else {
          _this[`${type}Disable`][i][k] = true
        }
      }
    }
    for (let i = 0; i < _this[`${type2}Disable`].length; i++) {
      for (const k in _this[`${type2}Disable`][i]) {
        _this[`${type2}Disable`][i][k] = true
      }
    }
    _this.disableFlag1 = false
    _this.disableFlag1 = false
    _this.disableFlag1 = false
  }
}

// 修改提示
export function listStatusTips(_this, obj) {
  _this.listStatus = {
    list: obj.list,
    code: obj.code,
    tips: obj.tips
  }
  _this.dialogStstusVisible = true
}

// 交流通道信息面板 id
export function sendContent(_this, type, index) {
  // 交流input的内容
  let list = []
  switch (type) {
    case 'Ac':
      list = [
        { command_id: `000${_this.AcArr[index]}0003`, value: '' }, // 设备名称
        { command_id: `000${_this.AcArr[index]}0007`, value: '' }, // 国标编码
        { command_id: `000${_this.AcArr[index]}0008`, value: '' }, // 第三方关联编码
        { command_id: `000${_this.AcArr[index]}0004`, value: '' }, // 目的ip地址
        { command_id: `000${_this.AcArr[index]}0005`, value: '' }, // 子网掩码
        { command_id: `000${_this.AcArr[index]}0006`, value: '' } // 端口
      ]
      break
    case 'Dc':
      list = [
        { command_id: `000${_this.DcArr[index]}0005`, value: '' }, // 设备名称
        { command_id: `000${_this.DcArr[index]}000d`, value: '' }, // 国标编码
        { command_id: `000${_this.DcArr[index]}000e`, value: '' }, // 第三方关联编码
        { command_id: `000${_this.DcArr[index]}0006`, value: '' }, // 目的ip地址
        { command_id: `000${_this.DcArr[index]}0007`, value: '' }, // 子网掩码
        { command_id: `000${_this.DcArr[index]}000c`, value: '' }, // 端口
        { command_id: `000${_this.DcArr[index]}000b`, value: '' }, // 连续时间
        { command_id: `000${_this.DcArr[index]}0001`, value: '' }, // 设备保护最小电流
        { command_id: `000${_this.DcArr[index]}0002`, value: '' }, // 设备保护最大电流
        { command_id: `000${_this.DcArr[index]}0003`, value: '' }, // 电流AD偏置
        { command_id: `000${_this.DcArr[index]}0004`, value: '' } // 电流放大倍数
      ]
      break
  }
  return list
}
// 直流通道信息面板 id
export function sendDcContent(_this, index) {
  const list = [
    { command_id: `000${_this.DcArr[index]}0005`, value: '' }, // 设备名称
    { command_id: `000${_this.DcArr[index]}000d`, value: '' }, // 国标编码
    { command_id: `000${_this.DcArr[index]}000e`, value: '' }, // 第三方关联编码
    { command_id: `000${_this.DcArr[index]}0006`, value: '' }, // 目的ip地址
    { command_id: `000${_this.DcArr[index]}0007`, value: '' }, // 子网掩码
    { command_id: `000${_this.DcArr[index]}000c`, value: '' }, // 端口
    { command_id: `000${_this.DcArr[index]}000b`, value: '' }, // 连续时间
    { command_id: `000${_this.DcArr[index]}0001`, value: '' }, // 设备保护最小电流
    { command_id: `000${_this.DcArr[index]}0002`, value: '' }, // 设备保护最大电流
    { command_id: `000${_this.DcArr[index]}0003`, value: '' }, // 电流AD偏置
    { command_id: `000${_this.DcArr[index]}0004`, value: '' } // 电流放大倍数
  ]
  return list
}
// 直流通道控制面板id
export function getlist1(_this, content) {
  const list = []
  for (let i = 0; i < content; i++) {
    list.push(
      {
        command_id: `000${_this.DcArr[i]}0005`,
        value: ''
      },
      {
        command_id: `000${_this.DcArr[i]}000a`,
        value: ''
      },
      {
        command_id: `010200${_this.OnOrInSwitch[i]}`,
        value: ''
      },
      {
        command_id: `000${_this.DcArr[i]}0008`,
        value: ''
      },
      {
        command_id: `000${_this.DcArr[i]}0009`,
        value: ''
      }
    )
  }
  // 发送数据
  send(_this, '01', list)
  _this.till_1 = true
}

// 交流通道控制面板id
export function getlist2(_this, content) {
  const list = []
  for (let i = 0; i < content; i++) {
    list.push(
      {
        command_id: `000${_this.AcArr[i]}0003`,
        value: ''
      },
      {
        command_id: `000${_this.AcArr[i]}0002`,
        value: ''
      },
      {
        // command_id: `010f000${i + 5}`,
        command_id: `0102001${i + 5}`, // 更新于2021/04/26
        value: ''
      },
      {
        command_id: `000${_this.AcArr[i]}0001`,
        value: ''
      }
    )
  }
  // 发送数据
  send(_this, '01', list)
  _this.till_2 = true
}

// 获取修改目的ip和端口
export function getSuccess(_this, id) {
  var index = _this[`current${_this.type}Index`]
  var num = _this[`${_this.type}Arr`][index]
  var ip = ''
  var dk = ''
  var list = {}
  switch (_this.type) {
    case 'Ac':
      ip = '4'
      dk = '6'
      break
    default:
      ip = '6'
      dk = 'c'
      break
  }
  // IP地址
  if (id === `000${num}000${ip}`) {
    console.log(id, `000${num}000${ip}`, `000${num}000${dk}`)
    list = {
      id: id,
      key: 'IP',
      content: _this[`${_this.type}Content`][index][1][`R_${id}`]
    }
  }
  // 端口
  if (id === `000${num}000${dk}`) {
    list = {
      id: id,
      key: 'GLDK',
      content: _this[`${_this.type}Content`][index][1][`R_${id}`]
    }
  }
  return list
}

// 初始化直流控制面板按钮样式
export function initDcSwitch(_this, index) {
  // 判断开关disable状态
  const count1 = Number(_this.Dc[index][`R_010200${_this.OnOrInSwitch[index]}`])
  const count2 = Number(_this.Dc[index][`R_000${_this.DcArr[index]}000a`])
  switch (count1) {
    // 直流跳合闸
    case 1:
      _this.DcDisable[2][`d${index}`] = false
      _this.DcDisable[3][`d${index}`] = false
      if (_this.DcDisable[1][`d${index}`]) {
        _this.DcDisable[2][`d${index}`] = true
        _this.DcDisable[3][`d${index}`] = true
      }
      break
    default:
      _this.DcDisable[2][`d${index}`] = true
      _this.DcDisable[3][`d${index}`] = true
  }
  switch (count2) {
    // 通道使能
    case 1:
      _this.DcDisable[1][`d${index}`] = false
      _this.disableFlag1 = false
      _this.disableFlag2 = false
      _this.disableFlag3 = false
      break
    default:
      _this.DcDisable[1][`d${index}`] = true
      _this.DcDisable[2][`d${index}`] = true
      _this.DcDisable[3][`d${index}`] = true
      _this.disableFlag1 = true
      _this.disableFlag2 = true
      _this.disableFlag3 = true
  }
}

// 初始化交流控制面板按钮样式
export function initAcSwitch(_this, index) {
  // 通道使能开关
  const count1 = Number(_this.Ac[index][`R_000${_this.AcArr[index]}0002`])
  // const count2 = Number(_this.Ac[index][`R_010f000${index + 5}`])
  const count2 = Number(_this.Ac[index][`R_0102001${index + 5}`]) // 更新于2021/04/26
  switch (count1) {
    case 1: // 开
      _this.AcDisable[1][`d${index}`] = false
      _this.disableFlag1 = false
      _this.disableFlag2 = false
      break
    default:
      _this.AcDisable[1][`d${index}`] = true
      _this.AcDisable[2][`d${index}`] = true
      _this.disableFlag1 = true
      _this.disableFlag2 = true
  }
  // 交流跳合闸
  // switch (count2) {
  //   case 1: // 开
  //     if (_this.AcDisable[1][`d${index}`]) {
  //       _this.AcDisable[2][`d${index}`] = true
  //     } else {
  //       _this.AcDisable[2][`d${index}`] = false
  //     }
  //     break
  //   default:
  //     _this.AcDisable[2][`d${index}`] = true
  // }
  // 交流跳合闸 更新于 2021-05-12
  switch (count2) {
    case 0: // 开
      if (_this.AcDisable[1][`d${index}`]) {
        _this.AcDisable[2][`d${index}`] = true
      } else {
        _this.AcDisable[2][`d${index}`] = false
      }
      break
    default:
      _this.AcDisable[2][`d${index}`] = true
  }
}

// 重置开关
export function initSwitch(_this) {
  switch (_this.type) {
    case 'Ac':
      initAcSwitch(_this, _this[`current${_this.type}Index`])
      break
    default:
      initDcSwitch(_this, _this[`current${_this.type}Index`])
      break
  }
}

// 成功或者失败清除默认样式
export function reset(_this, type) {
  var condition
  var txsn
  const typeCase = type.toLowerCase()
  if (type === 'Ac') {
    // console.log('-------------------------------------')
    // console.log(`R_010f000${_this[`current${type}Index`] + 5}`)
    // console.log(`R_0102001${_this[`current${type}Index`] + 5}`)
    // console.log('-------------------------------------')
    condition = `R_0102001${_this[`current${type}Index`] + 5}` // 更新于2021/04/26
    // condition = `R_010f000${_this[`current${type}Index`] + 5}`
    txsn = 2
    // disable[`d${index}`]
    _this[`${type}Disable`][0][`d${_this[`current${type}Index`]}`] = false
    _this[`${type}Disable`][1][`d${_this[`current${type}Index`]}`] = false
    _this[`${type}Disable`][2][`d${_this[`current${type}Index`]}`] = false
    _this.disableFlag1 = false
    _this.disableFlag2 = false
  } else {
    condition = `R_010200${_this.OnOrInSwitch[_this[`current${type}Index`]]}`
    txsn = 'a'
    _this[`${type}Disable`][0][`d${_this[`current${type}Index`]}`] = false
    _this[`${type}Disable`][1][`d${_this[`current${type}Index`]}`] = false
    _this[`${type}Disable`][2][`d${_this[`current${type}Index`]}`] = false
    _this[`${type}Disable`][3][`d${_this[`current${type}Index`]}`] = false
    _this.disableFlag1 = false
    _this.disableFlag2 = false
    _this.disableFlag3 = false
  }
  for (let i = 0; i < _this[`${type}Icon`].length; i++) {
    _this[`${type}Icon`][i][`icon${_this[`current${type}Index`]}`] = ''
  }
  const item = _this[`${type}`][_this[`current${type}Index`]]
  // console.log(condition, [`${type}`], [_this[`current${type}Index`]], _this[`${type}`][_this[`current${type}Index`]])
  for (const k in item) {
    switch (k) {
      case condition: // 跳合闸
        if (Number(item[k]) === 1) {
          // 跳合闸等于1是开启
          _this[`${type}Disable`][2][`d${_this[`current${type}Index`]}`] = false
          if (type === 'Dc') {
            _this[`${type}Disable`][3][`d${_this[`current${type}Index`]}`] = false
          }
          if (_this[`${type}Disable`][1][`d${_this[`current${type}Index`]}`]) {
            _this[`${type}Disable`][2][`d${_this[`current${type}Index`]}`] = true
            if (type === 'Dc') {
              _this[`${type}Disable`][3][`d${_this[`current${type}Index`]}`] = true
            }
          }
        } else {
          // 跳合闸等于0是关闭
          _this[`${type}Disable`][2][`d${_this[`current${type}Index`]}`] = true
          if (type === 'Dc') {
            _this[`${type}Disable`][3][`d${_this[`current${type}Index`]}`] = true
          }
        }
        break
      case `R_000${_this[`${type}Arr`][_this[`current${type}Index`]]}000${txsn}`: // 通道使能
        _this[`${type}Disable`][0][`d${_this[`current${type}Index`]}`] = false
        if (Number(item[k]) === 1) {
          _this[`${type}Disable`][1][`d${_this[`current${type}Index`]}`] = false
          _this[`${type}Disable`][2][`d${_this[`current${type}Index`]}`] = false
          _this.disableFlag1 = false
          _this.disableFlag2 = false
          if (type === 'Dc') {
            _this.disableFlag3 = false
          }
        } else {
          _this[`${type}Disable`][1][`d${_this[`current${type}Index`]}`] = true
          _this[`${type}Disable`][2][`d${_this[`current${type}Index`]}`] = true
          _this.disableFlag1 = true
          _this.disableFlag2 = true
          if (type === 'Dc') {
            _this[`${type}Disable`][3][`d${_this[`current${type}Index`]}`] = true
            _this.disableFlag3 = true
          }
        }
        break
      default:
    }
  }
  _this.btnD1 = false
  _this.btnD2 = false
  _this.btnD3 = false
  _this[`${typeCase}ContentLoad`] = false
}
